<!-- 
	Created By Kha Yuen
	23/02/2013
-->

<%@ page import="com.checkfree.isolutions.customer.persist.*" %>
<%@ page import="com.checkfree.isolutions.statement.*" %>
<%@ page import="com.checkfree.isolutions.statement.persist.*" %>
<%@ page import="com.checkfree.isolutions.property.Properties" %>
<%@ page import="com.checkfree.isolutions.property.PropertiesConfig" %>
<%@ page import="com.checkfree.isolutions.utility.*" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.text.*" %>
<%@ page import="com.myebills.ebpsfrontend.cust.maxis.*" %>
<%@ page import="com.myebills.ebpsfrontend.cust.utils.*" %>
<%@ page import="com.myebills.ebpsfrontend.cust.controller.*" %>

<%@ include file="../../@functions.jsp" %>

<script language="JavaScript" SRC="js/checkData.js"></script>

<%
	// Provide access to decrypted (and plain-text) URL request parameters.
    final EbppxUrlManager urlManager = (EbppxUrlManager) request.getAttribute(EbppxUrlManager.REQUEST_ATTRIBUTE_NAME);
	
	final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);
	
	final String phoneIndex = (String)request.getAttribute("phoneIndex");
    final int intPhoneIndex = Integer.parseInt(phoneIndex);
    final String simNo = EbppxUtil.nullToEmpty((String)request.getAttribute("simNo"));
	
	final String repeatSectionIndex = (String)request.getAttribute("repeatSectionIndex");
    int intRepeatSectionIndex = 0;
    if(repeatSectionIndex!=null && !repeatSectionIndex.equals("")){
		intRepeatSectionIndex = Integer.parseInt(repeatSectionIndex);
	}
	
	final String returnedURL = (String) session.getAttribute("returnedURL");
	
	// TelDet Additional Variables
	final String stmtIndex = (String)session.getAttribute("stmtIndex");
    final String sectionKey = (String)request.getAttribute("sectionKey");
    final String repeatSectionKey = (String)request.getAttribute("repeatSectionKey");
    final String flags = (String)request.getAttribute("flags");
    final String searchField = (String)request.getAttribute("searchBy");
    final String searchVal1 = (String)request.getAttribute("searchVal1");
    final String searchVal2 = (String)request.getAttribute("searchVal2");
    final String search = (String)request.getAttribute("search");
    final int pageNo = Integer.parseInt((String)request.getAttribute("pageNo"));
    int sortBy = Integer.parseInt(flags.substring(0,1));
    int sortType = Integer.parseInt(flags.substring(1,2));
    int nextSortType = (sortType > 0)?0:1;
	//System.out.println("!!!!!!!!!!!!XXXXXXXXXXXXXXXXXXXXX"+request.getAttribute("grouped"));
    int grouped = Integer.parseInt((String)request.getAttribute("grouped"));
	//System.out.println(flags +" val1:" + searchVal1 +" val2:" +  searchVal2 +" search:" +  search);
	final com.checkfree.isolutions.statement.persist.Statement cfiStatement = (com.checkfree.isolutions.statement.persist.Statement)request.getAttribute(EbppxConstants.CFI_STATEMENT);
	final StatementSection cfiRootSection = (StatementSection)request.getAttribute("rootSection");
    //StatementSection cfiSection = cfiRootSection.getSection("DetailsOfChargesSection");
    StatementSection phoneNumberSection = (StatementSection)request.getAttribute("PhoneNumberSection");
	
	String phoneNo = "";
	String accno = "";
	String msisdn = "";
	
	final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);	
	if(ecsUser != null){
		msisdn = ecsUser.getMsisdn();
		if(msisdn != null){
			phoneNo = msisdn;
		}
	}
	
	String telDetUrl = "ChargesData_v1r00";
    telDetUrl +=   "?phoneIndex="+phoneIndex+ "&simNo="+simNo+ "&sectionKey="+ sectionKey + "&repeatSectionKey=" + EncryptionHelper.encodeURL(repeatSectionKey) + "&stmtIndex="+stmtIndex;
	
	int searchBy = 0;
    StringTokenizer st = new StringTokenizer(searchField,"|");
    if (st.hasMoreTokens()) {
        searchBy = Integer.parseInt(st.nextToken());
    }
	//System.out.println(searchBy);
	
	int dispCount = 14;
	
	StatementSection selectedSection = null;
    SectionProperties selectedSP = getSectionProperties(sectionKey);
    ArrayList selectedParentSPList = new ArrayList();
	
	// find parent section
    if (phoneNumberSection != null){

        Iterator iterator = selectedSP.parentSectionKeyList.iterator();
        StatementSection section0 = phoneNumberSection;
        StatementSection section1 = null;
        int k=0;
        while (iterator.hasNext()){
			k++;
            SectionProperties sp = getSectionProperties((String)iterator.next());
            if (section0 == null){
                break;
            }
					//System.out.println("mcb"+sp.type);
            if (sp.type.equals("Type00") || sp.type.equals("Type01")){

                section1 = section0.getSection(sp.name);
                if (section1 != null){
                    sp.section = section1;
                    selectedParentSPList.add(sp);
                    section0 = section1;
                    section1 = null;
                } else{
                    break;
                }
            }
            if (sp.type.equals("Type02")){
                section1 = section0.getSection(sp.name);
                if (section1 != null){
                    selectedSection = section1;
                    break;
                }
            }
            if (sp.type.equals("Type12")){
                section1 = section0.getSection(sp.name);
                if (section1 != null){
                    selectedSection = section1;
                    break;
                }
            }
            if (sp.type.equals("Type11")){
                section1 = section0.getRepeatSection(sp.name,intRepeatSectionIndex);
                if (section1 != null){
                    selectedSection = section1;
                    break;
                }
            }
            if (sp.type.equals("Type04")){
                int sectionCount = (int) section0.getSectionCount(sp.name);
                for (int i=0; i<sectionCount; i++){
                    section1 = section0.getRepeatSection(sp.name,i);

                    if (section1 != null){
                        String title_ENG = sp.title_ENG;
                        if (sp.title_ENG.startsWith("*")){
                            title_ENG = section1.getFieldText(sp.title_ENG.substring(1));
                        }
                        //if (title_ENG.equals(repeatSectionKey)){
                            sp.section = section1;
                            selectedParentSPList.add(sp);
                            section0 = section1;
                            section1 = null;
                            break;
                        //}
                    }
                }
            }
            if (sp.type.equals("Type06")){
                int sectionCount = (int) section0.getSectionCount(sp.name);
                for (int i=0; i<sectionCount; i++){
                    section1 = section0.getRepeatSection(sp.name,i);

                    if (section1 != null){
                        String title_ENG = sp.title_ENG;
                        if (sp.title_ENG.startsWith("*")){
                            title_ENG = section1.getFieldText(sp.title_ENG.substring(1));
                        }
                        //if (title_ENG.equals(repeatSectionKey)){
                            sp.section = section1;
                            selectedParentSPList.add(sp);
                            section0 = section1;
                            section1 = null;
                            break;
                        //}
                    }
                }
            }
            if (sp.type.equals("Type10")){
                int sectionCount = (int) section0.getSectionCount(sp.name);
                for (int i=0; i<sectionCount; i++){
                    section1 = section0.getRepeatSection(sp.name,i);

                    if (section1 != null){
                        String title_ENG = sp.title_ENG;
                        if (sp.title_ENG.startsWith("*")){
                            title_ENG = section1.getFieldText(sp.title_ENG.substring(1));
                        }
                        //if (title_ENG.equals(repeatSectionKey)){
                            sp.section = section1;
                            selectedParentSPList.add(sp);
                            section0 = section1;
                            section1 = null;
                            break;
                        //}
                    }
                }
            }
            if (sp.type.equals("Type03")||sp.type.equals("Type05")){
                int sectionCount = (int) section0.getSectionCount(sp.name);
                for (int i=0; i<sectionCount; i++){
                    section1 = section0.getRepeatSection(sp.name,i);
                    if (section1 != null){
                        String title_ENG = sp.title_ENG;
                        if (sp.title_ENG.startsWith("*")){
                            title_ENG = section1.getFieldText(sp.title_ENG.substring(1));
                        }

                        if (title_ENG.equals(repeatSectionKey)){
                            sp.section = section1;
                            selectedParentSPList.add(sp);
                            section0 = section1;
                            section1 = null;
                            break;
                        }
                    }
                }
            }
        }
        section0 = null;
        section1 = null;

    } // phoneNumberSection != null
	
/*********************************************************************/
// Get Total Section
/*********************************************************************/

        StatementSection totalSection = selectedSection.getSection("TotalSection");

/*********************************************************************/
// Display parent header
/*********************************************************************/

        Iterator iterator = selectedParentSPList.iterator();
%>

<div class="popup">
<%
        SectionProperties sp = (SectionProperties) iterator.next();
		String title_ENG = sp.title_ENG;
        if (sp.title_ENG.startsWith("*")){
            title_ENG = sp.section.getFieldText(sp.title_ENG.substring(1));
        }
			
			double totalCharges = 0;
			totalCharges = getFieldAmount(totalSection, "Total");
%>

			<div class="title"><label><%=title_ENG%></label><sup>RM</sup><%=formatAmount(totalCharges)%></div>
<%
        


%>
	<div class="table" style="display: inline;">
		<?php /* This should be according to what you click above */ ?>
		<form action="<%= telDetUrl + "&flags="+sortBy+sortType+"&pageNo=1"%>" method="get" name = "searchForm" id="search_Form">
		<input type="hidden" name="search" value="1" class="text" id="searchHidden">
		<div class="table-filter">
			<p><label>Filter</label>
			<span class="select-custom filter">
				<select name="searchBy">
					<option value="">Select filter</option>
					<%	
						ArrayList fieldPropertiesList = getSectionFields(selectedSP.fieldKeyList);
						ArrayList callDetailRecordList = new ArrayList();
						//System.out.println(grouped);
						if (search.length() != 0){
							sortBy = 0;
							sortType = 0;
							grouped = 0;
							session.setAttribute("grouped","0");
						}
						
						int cnt = 1;
						iterator = fieldPropertiesList.iterator();			
						
						while (iterator.hasNext()){
							String format = "";
							FieldProperties fp = (FieldProperties) iterator.next();
							
							String selected = "";
							if (cnt == searchBy){
								selected = "selected";
							}	
							
							if (fp.type.equals("Date")){
								format = "(dd/mm/yy hh:mm:ss)";
							} else
							if (fp.type.equals("Double")){
								format = "(999.99)";
							}				
							
							if (fp.name.equals("Date") && (cnt == 0)){
					%>
								<option value="<%=(cnt++) + "|Date & Time|" + fp.type%>" <%=selected%>>Date &amp; Time</option>
					<%
							} 
							else {		
								String styleHTML = "";
								if(fp.name.equals("GrossAmount") || fp.name.equals("Discount") || fp.name.equals("Time") || fp.name.equals("NonTaxable")){
									styleHTML = "display:none";
								}
					%>				
									<option value="<%=(cnt++) + "|" + fp.title_ENG + "|" + fp.type%>" style="<%=styleHTML%>" <%=selected%>><%=fp.name%> <%=format%></option>
					<%
							}	
						}
					%>
				</select>
			</span>
			<span class="selector">
				<label id="label_1" for="searchVal1"></label> <input type="text" name="searchVal1" value="<%= searchVal1 %>"> <label id="label_2" for="searchVal2" class="to"></label> <input type="text" name="searchVal2" value="<%= searchVal2 %>"> <input type="submit" value="Filter" class="btn small" />
				<input type="submit" value="<% if(grouped == 0) out.print("Group"); else out.print("Ungroup");%>" class="btn group small"/>
				<input type="hidden" value="<% if(grouped == 0) out.print("1"); else out.print("0");%>" name="grouped"/>
			</span>
			
			</p>
		</div> <!-- end of filter //-->
		</form>
		<div class="table-head">
			<table cellpadding="0" cellspacing="0" border="0">
				<thead>
					<tr>
					<%
						cnt = 0;
						int dateTimeCount = 0;
						iterator = fieldPropertiesList.iterator();
						while (iterator.hasNext()){
							cnt++;
							FieldProperties fp = (FieldProperties) iterator.next();
							
							if (fp.name.equals("Date") || fp.name.equals("Time")){
								dateTimeCount++;
								if(dateTimeCount == 2){
					%>
								<th>Date &amp; Time</th>
					<%
								}
							}
							else{
								if(!fp.name.equals("GrossAmount") && !fp.name.equals("Discount") && !fp.name.equals("NonTaxable")){
					%>
						<th <%if(fp.name.equals("NumberCalled")){out.print("width=86px");}%>><%=fp.title_ENG%></th>
					<%
								}
							}
						}
					%>
					</tr>
				</thead>
			</table>
		</div>
<%		
/*********************************************************************/
// Retrieve call details
/*********************************************************************/

        int sectionCount = (int)selectedSection.getSectionCount("RepeatSection");
        for (int index = 0; index < sectionCount; index++) {
            StatementSection repeatSection = selectedSection.getRepeatSection("RepeatSection", index);
            ArrayList callDetailRecord = new ArrayList();
            iterator = fieldPropertiesList.iterator();
            while (iterator.hasNext()){
                FieldProperties fp = (FieldProperties) iterator.next();
                String fieldData = repeatSection.getFieldText(fp.name);
                if (fieldData == null){
                    fieldData = "";
                }
                //System.out.println("*******************************************************************"+fieldData);
                if(fieldData.indexOf(",")>0){
					fieldData = EbppxUtil.removeChar(fieldData,',');
				}
				callDetailRecord.add(fieldData);

            }
            callDetailRecord.add("D");
            callDetailRecordList.add(callDetailRecord);
        } // for (int index = 0; index < sectionCount; index++)
		
/*********************************************************************/
// Manipulate data (sorting)
/*********************************************************************/

        if (flags.length()>2 && searchBy > 0){
            sortBy = searchBy-1;
        }

        Collections.sort(callDetailRecordList, new CallDetailsComparator((sortType > 0)?false:true,sortBy,fieldPropertiesList));

/*********************************************************************/
// Manipulate data (filter/search)
/*********************************************************************/
        if (!searchVal1.equals("")&&!searchVal1.equals("N/A")&&(searchBy > 0)){
            ArrayList tempList = new ArrayList();
            ArrayList callDetailRecord = null;
            iterator = callDetailRecordList.iterator();
            FieldProperties fp = (FieldProperties) fieldPropertiesList.get(searchBy-1);
            while (iterator.hasNext()){
                callDetailRecord = (ArrayList)iterator.next();
                String fieldData = ((String) callDetailRecord.get(searchBy-1)).toUpperCase();
                String srchVal1 = searchVal1;
                String srchVal2 = searchVal2;
                boolean ok = false;

                if (fp.type.equals("Date")){
					String fieldData1 = ((String) callDetailRecord.get(searchBy)).toUpperCase();
					
				
                    DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
                    if (!fieldData.equals("")){
                        String tmpData = fieldData;
                        if (tmpData.indexOf("-") >= 0){
                            tmpData = tmpData.substring(0,tmpData.indexOf("-"));
                            //System.out.println("---"+tmpData+"---");
                        }else{
							tmpData += " " + fieldData1;
						}
						
                        if (srchVal2.equals("")){
                            srchVal2 = "01/01/01";
                        }
						
                        Date dtVal = df.parse(tmpData);
                        Date dtVal1 = df.parse(srchVal1);
                        Date dtVal2 = df.parse(srchVal2);
                        if (dtVal.equals(dtVal1)){
                            ok = true;
                        } else if (dtVal.equals(dtVal2)){
                            ok = true;
                        } else if (dtVal.after(dtVal1) && dtVal.before(dtVal2)){
                            ok = true;
                        }
                    }
                }

                if (fp.type.equals("Time")){
                    if (fieldData.equals("")){
                        fieldData = "00:00:00";
                    }
                    if (srchVal2.equals("")){
                        srchVal2 = "00:00:00";
                    }
                    final int intVal  = getSeconds(fieldData);
                    final int intVal1 = getSeconds(srchVal1);
                    final int intVal2 = getSeconds(srchVal2);
                    if (intVal == intVal1){
                        ok = true;
                    } else if (intVal == intVal2){
                        ok = true;
                    } else if (intVal > intVal1 && intVal < intVal2){
                        ok=true;
                    }
                }

                if (fp.type.equals("String")){
					if(fp.name.equals("Period")){
						if (fieldData.equals(srchVal1.toUpperCase())){
							ok = true;
						}
					}else{
						if (fieldData.indexOf(srchVal1.toUpperCase()) >= 0){
							ok = true;
							//System.out.println("Asdfasdfasdf");
						}
					}
                }

                if (fp.type.equals("Double")){
                    if (fieldData.equals("")){
                        fieldData = "0.00";
                    }
                    if (srchVal2.equals("")){
                        srchVal2 = "0.00";
                    }
                    final double dblVal = Double.parseDouble(fieldData);
                    final double dblVal1 = Double.parseDouble(srchVal1);
                    final double dblVal2 = Double.parseDouble(srchVal2);
                    if (dblVal == dblVal1){
                        ok = true;
                    } else if (dblVal == dblVal2){
                        ok = true;
                    } else if (dblVal > dblVal1 && dblVal < dblVal2){
                        ok = true;
                    }
                }

                if (ok){
                    tempList.add(callDetailRecord);
                }
            }
            callDetailRecordList = tempList;
        }
		
/*********************************************************************/
// Manipulate data (grouping)
/*********************************************************************/
        if (true){
            ArrayList tempList = new ArrayList();
            ArrayList totalRecord = new ArrayList();
            ArrayList callDetailRecord = null;
            String lastValue = "Lendy";
            String currValue = "";
            int totalCalls = 0;
            int totalDuration = 0;
            double totalAmount = 0.00;
            double totalGrossAmount = 0.00;
            double totalDiscount = 0.00;
            boolean last = false;
            iterator = callDetailRecordList.iterator();
            while (iterator.hasNext() || !last){
                if (iterator.hasNext()){
                    callDetailRecord = (ArrayList)iterator.next();
                    currValue = (String) callDetailRecord.get(sortBy);
                    if (grouped != 1){
                        lastValue = currValue;
                    }
                } else{
                    last = true;
                }

                if (!lastValue.equals(currValue) || last){
                    if (!lastValue.equals("Lendy")){
                        // Add Total
                        //String ttlCalls = "";
                        totalRecord = new ArrayList();
                        Iterator iterator0 = fieldPropertiesList.iterator();
                        while (iterator0.hasNext()){
                            FieldProperties fp = (FieldProperties) iterator0.next();
                            String fieldData = "";
                            if (fp.name.equals("Date")){
                                String desc = "call";
                                if (totalSection != null){
                                    String items = totalSection.getFieldText("items");
                                    // Added by Meau
                                    //ttlCalls = totalSection.getFieldText("Calls");
                                    // end added by Meau
                                    if (items != null && !items.equals("")){
                                        desc = "item";
                                    }
                                }
                                if (totalCalls > 1){
                                    desc += "s";
                                }
                                fieldData = (new Integer(totalCalls)).toString() + " " + desc;
                                //fieldData = ttlCalls;
                                //System.out.println(fieldData);
                            }
                            if (fp.name.equals("Duration")){
                                fieldData = toTime(totalDuration);
                            }
                            if (fp.name.equals("GrossAmount")){
                                fieldData = formatAmount(totalGrossAmount);
                            }
                            if (fp.name.equals("Discount")){
                                fieldData = formatAmount(totalDiscount);
                            }
                            if (fp.name.equals("Total")){
                                fieldData = formatAmount(totalAmount);
                            }
                            totalRecord.add(fieldData);
                        }
                        totalRecord.add("T");
                        tempList.add(totalRecord);

                        // Reset Counters
                        totalCalls = 0;
                        totalDuration = 0;
                        totalGrossAmount = 0.00;
                        totalDiscount = 0.00;
                        totalAmount = 0.00;
                    }
                    lastValue = currValue;
                }

                if (!last){
                    tempList.add(callDetailRecord);
                    totalCalls++;
                    int size1 = callDetailRecord.size()-1;
                    for (int index1 = 0; index1 < size1; index1++) {
                        String fieldData = (String)callDetailRecord.get(index1);
                        FieldProperties fp = (FieldProperties) fieldPropertiesList.get(index1);
                        if (fp.name.equals("Duration")){
                            if (fieldData.equals("")) fieldData = "00:00:00";
                            totalDuration += getSeconds(fieldData);
                        }
                        if (fp.name.equals("GrossAmount")){
                            if (fieldData.equals("")) fieldData = "0.00";
                            totalGrossAmount += Double.parseDouble(fieldData);
                        }
                        if (fp.name.equals("Discount")){
                            if (fieldData.equals("")) fieldData = "0.00";
                            totalDiscount += Double.parseDouble(fieldData);
                        }
                        if (fp.name.equals("Total")){
                            if (fieldData.equals("")) fieldData = "0.00";
                            totalAmount += Double.parseDouble(fieldData);
                        }
                    }
                }
            }

            callDetailRecordList = tempList;
        }

/*********************************************************************/
// Display call details
/*********************************************************************/

        int size0 = callDetailRecordList.size();
        int startIndex = (((pageNo-1)*dispCount));
        int recordTypeIndex = fieldPropertiesList.size();

%>		
		<div class="table-content">
			<table cellpadding="0" cellspacing="0" border="0">
				<tbody>
					<%
						//Map phoneBookMap = PhoneBookDAO.getInstance().list(ecsUser.getUserId(),"B");
						for (int index0 = startIndex; (index0<(startIndex+dispCount))&&(index0<size0); index0++) {
							ArrayList callDetailRecord = (ArrayList)callDetailRecordList.get(index0);
							int size1 = callDetailRecord.size()-1;
							String groupStyle = "";
							String recordType = (String)callDetailRecord.get(recordTypeIndex);
							if(!recordType.equals("T")){
								int newCount = index0 + 1;
								if(newCount < size0){
									ArrayList callDetailRecord_1 = (ArrayList)callDetailRecordList.get(newCount);
									String recordType_1 = (String)callDetailRecord_1.get(recordTypeIndex);
									if(recordType_1.equals("T")){
										groupStyle = "grouped";
									}
								}
							
							//Check group
					%>
							<tr class="<%=groupStyle%>">
					<%
								Map datetimeList = new HashMap();
								for (int index1 = 0; index1 < size1; index1++) {
									String fieldData = (String)callDetailRecord.get(index1);
									String nextFieldData = "";
									if(size1!=index1+1){
										nextFieldData = (String)callDetailRecord.get(index1+1);
									}
									fieldData = fieldData.trim();
									//out.println(nextFieldData);
									FieldProperties fp = (FieldProperties) fieldPropertiesList.get(index1);
									int width = fp.width;
									int colspan = fp.colspan;
									/*if (((colspan > 1) && (fieldData.indexOf("-") >= 0) && (fp.type.equals("Date"))) || ((colspan > 1) && (!fp.type.equals("Date")))){
										if(nextFieldData.equals("")){
										  index1++;
										  FieldProperties nextFP = (FieldProperties) fieldPropertiesList.get(index1);
										  width += nextFP.width;
										}else{
										  colspan = 1;
										}
									} else{
										colspan = 1;
									}*/
									/*String alink1 = "";
									String alink2 = "";
									String phoneImg = "";
									if (fp.phoneBookTag.equals("Y") && (!fieldData.equals("Call Received")) && (!fieldData.equals("")) && userType.equals("Cus")){
										alink1 = "<A href='CusPhoneBookAdd_v1r00.jsp?pbFromPage=A&phoneBookNo="+fieldData+"'>";
										alink2 = "</A>";

										if(fp.name.equals("NumberCalled") && phoneBookMap.containsKey(fieldData) ){
											phoneImg = "<img src=\"images/business_phone_book.bmp\">";
										}
									}*/
									
									
									
									if(fp.name.equals("Date")){
										if(nextFieldData != ""){
							
%>
										<td><%=fieldData +", " +nextFieldData%></td>
<%									
										}
										else{ 
%>
											<td><%=fieldData%></td>
<%										
										}
									}
									else if(!fp.name.equals("Time") && !fp.name.equals("GrossAmount") && !fp.name.equals("Discount") && !fp.name.equals("NonTaxable")){
%>

                        <td <%if(fp.name.equals("NumberCalled")){out.print("width=100px");}%>><%=fieldData%></td>
<%
									}
								}
%>
						</tr>
<%
							}
						}
%>
				</tbody>
			</table>
		</div>
<%
/*********************************************************************/
// Display page selection
/*********************************************************************/

        int pageCount = size0/dispCount; // lines per page
        if ((size0%dispCount)>0){
            pageCount++;
        }
        int prevPage = 1;
        if (pageNo>1){
            prevPage = pageNo-1;
        }
        int nextPage = pageCount;
        if (pageNo<pageCount){
            nextPage = pageNo+1;
        }
		
%>	
		<div class="table-pagination">
<%			if(pageNo != 1){
%>
			<a href="<%=telDetUrl+"&flags="+sortBy+sortType+"&pageNo="+prevPage%>" class="prev">&lsaquo; Previous</a>
<%
	}
%>
<% 		if(pageCount > 0){ %>
			<span class="select-custom">
				<select name="page" id = "page">
<%		
			String url = "";
			for(int i=1;i<=pageCount;i++){
				url = telDetUrl+"&flags="+sortBy+sortType+"&pageNo="+i;
				if (pageNo == i) {
					out.println("<option value=\""+url+"\"selected>"+i+"</option>");
				} else {
					out.println("<option value=\""+url+"\">"+i+"</option>");
				}
			}
%>
				</select>
			</span>
<%		}%>
<%			if(pageCount > 1 && (pageCount != pageNo)){
%>
			<a href="<%=telDetUrl+"&flags="+sortBy+sortType+"&pageNo="+nextPage%>" class="next">Next &rsaquo;</a>
<%
	}
%>

		</div>
		<a href="<%=returnedURL%>" class="btn grey small back-button">Back</a>
	</div>
</div>

<script src="js/popup.js"></script>

<script>
$(function(){
	$("#search_Form").submit( function (event) {
		event.preventDefault();

		if(checkFields()){
			$.fancybox.close();
			$.ajax({
				beforeSubmit: $.fancybox.showLoading(),
				type: $("#search_Form").attr('method'),
				url: $("#search_Form").attr('action'),
				data: $("#search_Form").serialize(),
				success: function (data) {
					$.fancybox(data, {
						width		: 400,
						height		: 500,
						fitToView	: true,
						autoSize	: true,
						closeClick	: false,
						openEffect	: 'fade',
						closeEffect	: 'fade',
						padding		: 0
					});
				}
			});
		}
		
		return false;
	});
	

   $("#page").bind('change', function() {
       $.fancybox({
			'width': 800,
			'height': 700,
			'type': 'ajax',
			href:$('#page').val()}
		)
	});

	$('.next, .prev, .back-button').fancybox({
		type		: 'ajax',
		width		: 400,
		height		: 500,
		fitToView	: true,
		autoSize	: true,
		closeClick	: false,
		openEffect	: 'fade',
		closeEffect	: 'fade',
		padding		: 0
	});

	function showFilterInput(){
		var value = $(".table-filter .filter option:selected").val();

		if($(".selector option:selected").val() !== ""){
			var items = value.split("|"); 
		
			$("#label_1").text(items[1]);
			$("#label_2").text(items[1]);
			
			if(items.length === 3){
				if(items[2] === "String" ){
					$(".table-filter .selector input:text").datepicker( "destroy" );
					$(".table-filter .selector input:text").removeClass("hasDatepicker").removeAttr('id');
				
					$(".table-filter .selector input:text").eq(1).val("N/A");
					$(".table-filter .selector input:text").eq(1).hide();
					$("#label_2").hide();		
				}
				else{
					$("#label_1").text("From");
					$("#label_2").text("To");
				
					$(".table-filter .selector input:text").show();
					$(".table-filter .selector label").show();
					
					if(items[2] === "Date"){
						$(".table-filter").find(".selector input:text").addClass("datetimepicker");
						
						$('.datetimepicker').datetimepicker({
							controlType: 'select',
							dateFormat: 'dd/mm/y',
							timeFormat: 'HH:mm:ss'
						}); 
					}else{
						$(".table-filter .selector input:text").datepicker( "destroy" );
						$(".table-filter .selector input:text").removeClass("hasDatepicker").removeAttr('id');
					}
				}
			
				$(".table-filter .selector").show();
			}
		}
	}
	
	showFilterInput();
});
</script>





